package cn.yunyichina.provider.merchant.thread;

import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.iface.entity.merchant.NotifyMessageRequestDto;
import cn.yunyichina.provider.merchant.dao.MerchantMessageDao;
import cn.yunyichina.provider.merchant.entity.MerchantMessage;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.commons.IDUtils;
import cn.yunyichina.utils.log.Log;
import cn.yunyichina.utils.log.constant.LogScene;
import java.util.Date;

/**
 * Created by kevin on 2017/12/7.
 */
public class SaveMessageThread implements Runnable {

    private NotifyMessageRequestDto messageRequestDto;

    public SaveMessageThread(NotifyMessageRequestDto notifyMessageRequestDto) {
        this.messageRequestDto = notifyMessageRequestDto;
    }

    @Override
    public void run() {
        try {
            MerchantMessageDao merchantMessageDao = SpringContextHandler.getBean("merchantMessageDao");
            MerchantMessage merchantMessage = new MerchantMessage();
            merchantMessage.setId(IDUtils.getUUID32());
            merchantMessage.setOrgCode(messageRequestDto.getOrgCode());
            merchantMessage.setOrgName(messageRequestDto.getOrgName());
            merchantMessage.setNotifyScene(messageRequestDto.getNotifyScene().getCode());
            merchantMessage.setOrderNo(messageRequestDto.getOrderNo());
            merchantMessage.setMessageContent(messageRequestDto.getMessageContent());
            merchantMessage.setNotifyCount(0);
            merchantMessage.setNotifyFlag((short) 0);
            merchantMessage.setCreatedTime(DateUtils.dateToString(new Date()));
            merchantMessage.setHandleMessage("未处理");
            merchantMessageDao.insert(merchantMessage);
        } catch (Exception ex) {
            Log.e(LogScene.MERCHANT,messageRequestDto.getOrderNo(),"入库失败,异常原因:"+ex.getMessage(),ex);
        }
    }
}
